/**
 * 扫描 Nexus 仓库指定目录下的文件路径
 *
 * @param nexusUrl       Nexus 服务地址，例如 https://nexus.example.com
 * @param repositoryName 仓库名
 * @param credentialsId  Jenkins 中的用户名密码凭据 ID
 * @param pathPrefix     只获取 path 以此前缀开头的条目（可为 null 或 "" 表示全部）
 * @return List<String>  满足条件的路径列表
 */
def call(String nexusUrl, String repositoryName, String credentialsId, String groupId  ,String artifactId ) {
    def paths = []

    withCredentials([usernamePassword(
            credentialsId: credentialsId,
            usernameVariable: 'NX_USER',
            passwordVariable: 'NX_PWD')]) {
        echo "artifactId: ${artifactId}"

        def url = "${nexusUrl}/service/rest/v1/search/assets?repository=${repositoryName}&path=base&&limit=500"

        echo "Requesting: ${url}"

        def resp = sh(script: """
            curl -s -u "\$NX_USER:\$NX_PWD" '${url}'
        """, returnStdout: true).trim()

        echo "resp: ${resp}"

        def data = parseJson(resp)
        echo "data: ${data}"
        data.items.each { item ->
            if (!pathPrefix || item.path.startsWith(pathPrefix)) {
                paths << item.path
            }
        }
    }

    return paths
}

@NonCPS
def parseJson(String text) {
    def slurper = new groovy.json.JsonSlurper()
    return slurper.parseText(text)
}